/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.client.Connection;

import db.client.DTO.DatabaseDTO;
import db.client.DTO.LoginDTO;
import db.client.GUI.DBClient;
import db.client.GUI.DBClientLogin;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author QuangKhai
 */
///Get database list
public class DBClientThread implements Runnable{
    private ObjectOutputStream oos;
    private ObjectInputStream ois;
    private Socket socket;
    private LoginDTO loginDTO = new LoginDTO();
    public static List<DatabaseDTO> database = new ArrayList<DatabaseDTO>();
    private DBClient dbClient;
    private DBClientLogin dbLogin;
    public DBClientThread(LoginDTO loginDTO, DBClient dbClient, DBClientLogin dbLogin) throws IOException{
        this.dbClient = dbClient;
        this.dbLogin = dbLogin;
        this.loginDTO = loginDTO;
        this.socket = new Socket(loginDTO.getAddress(), loginDTO.getPort());        
        oos = new ObjectOutputStream(socket.getOutputStream());        
        ois = new ObjectInputStream(socket.getInputStream());        
        oos.flush();
    }
    
    public void sendType() throws IOException{
        oos.writeUTF("Client");
        oos.flush();
    }
    
    public void sendClient() throws IOException{
        oos.writeUTF(loginDTO.getUsername());
        oos.flush();
        oos.writeUTF(loginDTO.getPassword());
        oos.flush();                
    }
    
    public boolean receiveState() throws IOException{
        String result = ois.readUTF();             
        
        if(result.compareTo("Kết nối thành công") == 0){
            return true;
        }else{
            return false;
        }
    }
    
    public List<String> recieveDatabase() throws IOException, ClassNotFoundException{
        List<String> lstDatabaseName = new ArrayList<String>();
        List<List<String>> lstDatabaseColumns = new ArrayList<List<String>>();
        
        int iDBCount = ois.readInt();        
        for(int i = 0; i < iDBCount; i++){            
            lstDatabaseName.add(ois.readUTF());
            
            int iTables = ois.readInt();
            List<String> lstColumns = new ArrayList<String>();
            for(int j = 0; j < iTables; j++){
                lstColumns.add(ois.readUTF());
                
                int iColumns = ois.readInt();
                for(int k = 0; k < iColumns; k++){
                    lstColumns.add(ois.readUTF());
                }
            }
            
            lstDatabaseColumns.add(lstColumns);
        }
        
        dbClient.lstColumns = lstDatabaseColumns;
        
        return lstDatabaseName;
    }
    
    @Override
    public void run() {
        try {
            sendType();
            sendClient();
            if(receiveState()){
                List<String> databaseRecieve = recieveDatabase();  
                
                dbClient.databaseName = databaseRecieve;
                dbClient.socket = socket;
                dbClient.oos = oos;
                dbClient.ois = ois;
                //dbClient.isConnected = true;
                dbLogin.connected();
            }else{
                JOptionPane.showMessageDialog(null, "Đăng nhập không hợp lệ");
            }
        } catch (IOException ex) {
            Logger.getLogger(DBClientThread.class.getName()).log(Level.SEVERE, null, ex);
        }catch (ClassNotFoundException ex) {
            Logger.getLogger(DBClientThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
}
